package cn.gene.timeyan.ums.webapi.ums.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis配置类
 */
@Configuration
public class RedisConfiguration {
    @Value("${spring.redis.database.db1}")
    private int db1;
    @Value("${spring.redis.database.db2}")
    private int db2;
    @Value("${spring.redis.database.db3}")
    private int db3;
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    //用于存储用户信息
    @Bean(name = "redisTemplate1")
    public RedisTemplate<String,Object> redisTemplate1(){
        LettuceConnectionFactory factory=new LettuceConnectionFactory(getRedisStandaloneConfiguration(db1));
        factory.afterPropertiesSet();
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
    //用于存储浏览记录
    @Bean(name = "redisTemplate2")
    public RedisTemplate<String,Object> redisTemplate2(){
        LettuceConnectionFactory factory=new LettuceConnectionFactory(getRedisStandaloneConfiguration(db2));
        factory.afterPropertiesSet();
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
    @Bean(name = "redisTemplate3")
    public RedisTemplate<String,Object> redisTemplate3(){
        LettuceConnectionFactory factory=new LettuceConnectionFactory(getRedisStandaloneConfiguration(db3));
        factory.afterPropertiesSet();
        RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }
    private RedisStandaloneConfiguration getRedisStandaloneConfiguration(int database){
        RedisStandaloneConfiguration configuration=new RedisStandaloneConfiguration();
        configuration.setHostName(host);
        configuration.setPort(port);
        configuration.setDatabase(database);
        return configuration;
    }
}
